home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 192_01 / tds.c < prev    next >
Text File  |  1980-01-01  |  12KB  |  354 lines

  1. /*   (C) Copyright 1985, 1986 Neil V. Deasy                             
  2.         Program Name: Terminal Digit Sorter
  3.           Where Used: In hospital's Medical Records Department.  It speeds
  4.             the task of pulling and re-filing records into
  5.             terminal digit (non-numeric) order.
  6.             Features: Program is driven by the key words Sort, Exit, Help.
  7.             Anything else is checked upon input to meet the
  8.             expected criteria of numbers of six digits length.
  9.             Improper entry is rejected and an error message
  10.             explaines why.  Record numbers are not sorted until
  11.             specified, and if not specified, are saved in the
  12.             file 'DATA.DAT'.  When the operator requests sorting
  13.             all numbers are sorted, that includes ones previously
  14.             stored as well as those which may have just been
  15.             entered at the keyboard.  The Record numbers are output
  16.             on the printer, with page headers and numbering, in
  17.             their peculiar but proper order.  Beside each Record
  18.             number is printed a line, so that patient names or
  19.             other marginal notes can be made.
  20.  
  21.    ====================================================================  ;
  22. ;   This program sorts numbers into terminal digit order. The numbers    ;
  23. ;   are actually strings, since it is necessary to partition them for    ;
  24. ;   sorting.                                                             ;
  25. ;   Present limit on entries is 600, a considerable number.  Version 1.0 ;
  26. ;   lacked a 'help' screen. Version 1.3 fixed that and added video       ;
  27. ;   attributes.  Version 1.5 fixed an unnecessary 'beep' and provided    ;
  28. ;   nicely paginated output.                                             ;
  29. ;   Version 1.6 adds color and 'boxes' the help text.                    ;
  30. ;                                                                        ;
  31. ;    Eco-C88 is a copyright of Ecosoft, Inc., 1983 - 1986.             ;
  32. ; ===================================================================== */
  33.  
  34. /*  Uses functions available with the Ecosoft Eco-C88 Compiler  Ver 3.00  */
  35. /*  Runs on the IBM and most generics, uses color or monochrome which  */
  36. /*  includes the Hercules clones.  Printer is assumed to be Epson-code  */
  37. /*  compatible, like the Gemini and other printers.  The routines which */
  38. /*  bold, underline or reverse video the text assume ANSI.SYS is installed. */
  39.  
  40.  
  41. #include "stdio.h"
  42. #include "color.h"
  43. #include "string.h"
  44. #define BELL 7
  45. #define ESC 27
  46. int _colorflag = TRUE;   /* says you have color, but works if you don't */
  47. char bigbuff[4200]; /* the size of bigbuff determines how many records  */
  48. int strcmp();       /* can be brought into memory and sorted, in this   */
  49. int cnt, flag, page; /* case the size is 4200/7 = 600 Record numbers,   */
  50. unsigned row, col;   /* see msg4();, elsewhere in this program          */
  51. FILE *fd;
  52.  
  53. main()
  54. {
  55. char *gets();
  56. char s[7];       /* holds 6 digits plus the \0 string terminator */
  57. char *strcpy();
  58. int c, p;
  59. flag = cnt = 0;
  60. row = 4;
  61. col = 3;
  62.     fd = fopen("data.dat","a");     /* opening and/or creating this file */
  63.     if((c = ferror(fd)) != NULL)    /* should produce no problem, however*/
  64.     {                               /* this message is a warning   */
  65.         puts("A file error has occured...");
  66.         exit();
  67.     }
  68.     clrscr();
  69.     cursor(0,29);
  70.     bld();            
  71.     mascr(2,FRED,BOLD);
  72.     puts("TERMINAL DIGIT SORTER");
  73.     unbld();
  74.     cursor(22,27);
  75.     revon();
  76.     mascr(1,FGREEN);
  77.     puts("enter Exit or Sort or Help");
  78.     revoff();
  79.     cursor(23,21);
  80.     bld();
  81.     mascr(2,FBLUE,BOLD);
  82.     puts("(C) Copyright 1985, 1986 Neil V. Deasy");
  83.     unbld();
  84.     mascr(1,FWHITE);
  85. loop:    cursor(row,col);
  86. while(TRUE)    {
  87.     printf("Enter number or selection  ");
  88.     gets(s);
  89.     if(flag != 0)    {   /*  flag is set by error routines */
  90.         eraeol(cursor(24,0));  /* so at this point in the program */
  91.         flag = 0;   /* we erase the error message and reset the */
  92.         }                          /*    flag   */
  93.     cursor(++row,col);
  94.     eraeol(row,col);
  95.     if(tolower(s[0]) == 'e' || tolower(s[0]) == 's' || tolower(s[0])=='h')
  96.     {
  97.         switch(tolower(s[0]))
  98.         {                           /*  here are your choices  */
  99.             case 'e':
  100.                 clrscr();
  101.                 fclose(fd);
  102.                 exit();
  103.             case 's':
  104.                 fclose(fd);
  105.                 append1();
  106.                 exit();
  107.             case 'h':
  108.                 help();
  109.                 printf("\nEnter <CR> to continue. . . .");
  110.                 gets(s);
  111.                 eraeol(cursor(21,0)); /* erase the help msg */
  112.                 for(row = 20; row > 4; row--) /* by backing */
  113.                     eraeol(cursor(row,0));/* up 15 lines*/
  114.                 row = 4;
  115.                 col = 0;
  116.                 eraeol(cursor(row,col));
  117.                 goto loop;
  118.         }
  119.     }      /*  input MUST be 6 characters  */
  120.     if((p = strlen(s)) < 6)  /* this input is less than 6  */
  121.         msg1();
  122.     if((p = strlen(s)) > 6)  /* this input is more than 6  */
  123.         msg2();
  124.     if((p = strlen(s)) ==6)  /* this is exactly 6, BUT  */
  125.     {
  126.         int w;
  127.         for(w = 0; w < 6; w++)   /* let's check each to make */
  128.         if((c = isdigit(s[w])) == 0) /* sure there are NO letters */
  129.         {
  130.             msg3();  /*  it isn't a digit!  */
  131.         }
  132.     }
  133.     if(flag == 0)
  134.         {                           /* since we got here with no */
  135.         fprintf(fd,"%6s\n",&s[0]);  /* errors - write it to the file */
  136.             cnt += 1;
  137.         }
  138.     if(cnt > 14)   /* screen display area is now at max of 15 lines  */
  139.     {
  140.         for(row = 4; row < 20; row++)  /* so clear screen and  */
  141.             eraeol(cursor(row,0));
  142.         cnt = 0; row = 4; col = 3;     /* start over again  */
  143.         cursor(row,col);
  144.         }
  145.     }
  146. }
  147. msg1()   /* messages follow a similar format, see below  */
  148. {
  149.     flag = 1;                    /*  set error flag */
  150.     cursor(--row,col);           /* adjust row count location */
  151.     cursor(24,0);                /* bottom of screen for error message */
  152.     bld();                       /*  attribute on  */
  153.     puts("Error: too FEW digits (6 needed) "); /* what U did wrong */
  154.     unbld();                     /* reset attribute  */
  155.     eraeol(cursor(row,30));      /*  erase prompting line  */
  156.     cursor(row,col);             /* put cursor at start of prompt line  */
  157. putchar(BELL);                       /* audible indication of error  */
  158.     return(flag);                /* says it all */
  159. }
  160. msg2()
  161. {
  162.     flag = 1;
  163.     cursor(--row,col);
  164.     cursor(24,0);
  165.     bld();
  166.     puts("Error: too MANY digits (6 needed)");
  167.     unbld();
  168.     eraeol(cursor(row,30));
  169.     cursor(row,col);
  170. putchar(BELL);
  171.     return(flag);
  172. }
  173. msg3()
  174. {
  175.     flag = 1;
  176.     cursor(--row,col);
  177.     cursor(24,0);
  178.     bld();
  179.     puts("Error: input must be NUMBERS     ");
  180.     unbld();
  181.     eraeol(cursor(row,30));
  182.     cursor(row,col);
  183. putchar(BELL);
  184.     return(flag);
  185. }
  186. msg4()
  187. {
  188.     clrscr();
  189.     puts("TDS will not sort more than 600 numbers.");
  190.     puts("Delete some entries from file 'DATA.DAT' and re-run TDS.");
  191.     exit();
  192. }
  193. append1()
  194. {
  195.     char temp[2];   /* a 2 character swap buffer */
  196.     int x, y;
  197.     x = y = 0;
  198.  
  199.     fd = fopen("data.dat","r");
  200.     clrscr();
  201.     bld();
  202.     puts("READING DATA FILE");
  203.     unbld();
  204.     if((y = fgetc(fd)) == EOF)   /* see if file is empty to start */
  205.     {
  206.         puts("You made no entries to sort.        Program ends.");
  207.         exit();              /* it was, so exit  */
  208.     }
  209.     ungetc(y,fd);                /* it wasn't, so put back a character */
  210.     while((y = feof(fd))==FALSE)    {  /* read file till EOF detected */
  211.         fscanf(fd,"%6s\n",&bigbuff[x]);
  212.         strncpy(&temp[0],&bigbuff[x+4],2);    /* put last 2 char's */
  213.         strncpy(&bigbuff[x+4],&bigbuff[x+2],2); /* up front and */
  214.         strncpy(&bigbuff[x+2],&bigbuff[x],2); /* slide the others  */
  215.         strncpy(&bigbuff[x],&temp[0],2);      /* over to align */
  216.         x += 7;   /* bump count by 7 to include the '\0'  */
  217.     }
  218.     fclose(fd);
  219.     if((x/7) > 600)
  220.         msg4();
  221.     printf("sorting %d entries\t\t",x/7);  /* a visual marker of progress*/
  222. qsort(&bigbuff,(x/7),7,strcmp);     /* a library sort, using string compare  */
  223. bld();
  224. puts("Sorted!");
  225. unbld();
  226. header(&bigbuff,x);     /*  print page header */
  227. fflush(stdlst);
  228. unlink("data.dat");     /* discard the file now that it's printed */
  229. }
  230. header(buf,x)
  231. int x;
  232. {
  233.     int  y, loop, page;
  234.     char temp[2];
  235.     y = loop = 0;
  236.     page = 1;
  237.     dump(page); /* this function prints page header info only */
  238.     for(y = 0; y < x; y++)
  239.     {  /* print sorted Record Numbers  */
  240.         strncpy(&temp[0],&bigbuff[y],2);        /* now that they